package com.zg.dao;

import com.zg.datasource.ZgDataSource;
import com.zg.entity.Dm;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 字典表
 *
 * @author 许洋
 *         2016/6/20.
 */
@Repository
public class DmDaoImpl extends ZgDataSource<Dm> implements DmDao {

    @Override
    public List<Dm> listTheme() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='XCLX' ");
    }

    @Override
    public List<Dm> listFlzxTheme() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='ZXSX' ");
    }

    @Override
    public List<Dm> getFwjgThem() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='JGLX' ORDER BY dmbm ");
    }

    @Override
    public List<Dm> listYwyy() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='YYSX'");
    }

    @Override
    public List<Dm> listRmtjTheme() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='JFLX'");
    }

    @Override
    public List<Dm> getBfjlTheme() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='RYLX_AZBJ'");
    }

    @Override
    public List<Dm> getSqjzTheme() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='RYLB_SQJZ'");
    }


    @Override
    public List<Dm> getRylx() {
        return list("select dmlb,dmmc,dmbm,dmms,sssjbm,px,zt,jlbh from dm where dmlb='RYLX' ");
    }

    @Override
    public String getDmmcByDmbm(String type, String dmbm) {
        return jdbcTemplate.query("SELECT dmmc FROM dm WHERE dmlb=? AND dmbm=?", new Object[]{type, dmbm}, new ResultSetExtractor<String>() {
            @Override
            public String extractData(ResultSet rs) throws SQLException, DataAccessException {
                while (!rs.next()) {
                    return "";
                }
                return rs.getString("dmmc");
            }
        });
    }

    @Override
    public List<Dm> listByDmlb(String dmlb) {
        return list("SELECT dmmc,dmbm FROM dm WHERE dmlb = ?",dmlb);
    }
}